package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.business.VO.SharingVO;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BuSharingMapper;
import com.ruoyi.business.domain.BuSharing;
import com.ruoyi.business.service.IBuSharingService;

/**
 * 绑定分润配置 Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@Service
public class BuSharingServiceImpl implements IBuSharingService
{
    @Autowired
    private BuSharingMapper buSharingMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询绑定分润配置
     *
     * @param id 绑定分润配置 主键
     * @return 绑定分润配置
     */
    @Override
    public BuSharing selectBuSharingById(Long id)
    {
        return buSharingMapper.selectBuSharingById(id);
    }

    /**
     * 查询绑定分润配置 列表
     *
     * @param buSharing 绑定分润配置
     * @return 绑定分润配置
     */
    @Override
    public List<BuSharing> selectBuSharingList(BuSharing buSharing)
    {
        return buSharingMapper.selectBuSharingList(buSharing);
    }

    @Override
    public List<SharingVO> selectBuSharingList2(BuSharing buSharing)
    {
        List<SharingVO> sharingVOList = new ArrayList<>();
        List<BuSharing> buSharingList = buSharingMapper.selectBuSharingList(buSharing);
        for (BuSharing buSharing1 : buSharingList) {
            SharingVO sharingVO = new SharingVO();
            sharingVO.setId(buSharing1.getId());
            sharingVO.setCustomId(buSharing1.getCustomId());
            sharingVO.setUserId(buSharing1.getUserId());
            sharingVO.setProfitSharing(buSharing1.getProfitSharing());
            //查出用户名，职位
            SysUser sysUser = userService.selectUserById(buSharing1.getUserId());
            if (sysUser != null){
                sharingVO.setNickName(sysUser.getNickName());
                sharingVO.setPostName(sysUser.getPostName());
                /*SysDept sysDept = deptService.selectDeptById(sysUser.getDeptId());
                //小组
                if (sysDept.getLevel()==4){
                    sysUser.setTeamName( sysDept.getDeptName() );
                    SysDept sysDeptA = deptService.selectDeptById(sysDept.getParentId());
                    sysUser.setDeptName( sysDeptA.getDeptName() );
                    sharingVO.setDeptName( sysDept.getDeptName() );
                }else {
                    sysUser.setDeptName( sysDept.getDeptName() );
                    sharingVO.setDeptName( sysDept.getDeptName() );
                }*/
            }
            sharingVOList.add(sharingVO);
        }
        return sharingVOList;
    }

    /**
     * 新增绑定分润配置
     *
     * @param buSharing 绑定分润配置
     * @return 结果
     */
    @Override
    public int insertBuSharing(BuSharing buSharing)
    {
        return buSharingMapper.insertBuSharing(buSharing);
    }

    /**
     * 修改绑定分润配置
     *
     * @param buSharing 绑定分润配置
     * @return 结果
     */
    @Override
    public int updateBuSharing(BuSharing buSharing)
    {
        return buSharingMapper.updateBuSharing(buSharing);
    }

    /**
     * 批量删除绑定分润配置
     *
     * @param ids 需要删除的绑定分润配置 主键
     * @return 结果
     */
    @Override
    public int deleteBuSharingByIds(Long[] ids)
    {
        return buSharingMapper.deleteBuSharingByIds(ids);
    }

    /**
     * 删除绑定分润配置 信息
     *
     * @param id 绑定分润配置 主键
     * @return 结果
     */
    @Override
    public int deleteBuSharingById(Long id)
    {
        return buSharingMapper.deleteBuSharingById(id);
    }
}
